function enableFieldForEmployeeUpdate(theForm) {
	document.getElementById('employeeName').disabled=false;
	document.getElementById('emailId').disabled=false;
	document.getElementById('password').disabled=false;
	document.getElementById('roleId').disabled=false;
	document.getElementById('passportNo').disabled=false;
	document.getElementById('ppIssuingAuthority').disabled=false;
	document.getElementById('ppExpiryDate').disabled=false;
	document.getElementById('visaStatus').disabled=false;
	document.getElementById('reminderEmailCc').disabled=false;
	document.getElementById('employeeStatusOnChange').disabled=false;
	document.getElementById('saveUpdateButton').style.display='block';
	document.getElementById('updateButton').style.display='none';
	document.getElementById('employeeName').focus();
}
function validateonSearchSubmit(theForm) {
	return true;
}

function validateOnInactive(){
	
	var status = document.getElementById("employeeStatusOnChange").value;
	if(status==0){
		var result = confirm("Inactivating employee will inactivate all associated records.Do you want to continue?");
		if(result == true){
			return true;
		}else{
			return false;
		}
	}else{
		return true;
	}
	
}

function validateFormBeforeSubmitForUpdateEmployee(theForm){
	var reason = "";
	var employeeCode = validateMe(document.getElementById('employeeCode'),'Employee Code');
	reason += employeeCode;
	var employeeName = validateMe(document.getElementById('employeeName'),'Employee Name');
	reason += employeeName;
	var emailId = validateMe(document.getElementById('emailId'),'Email Id');
	reason += emailId;
	var password = validateMe(document.getElementById('password'),'Password');
	reason += password;
	var roleId = validateMe(document.getElementById('roleId'),'Role Id');
	reason += roleId;
	
	if (reason != "") {
		alert("Errors:\n" + reason); 
		return false;
	} 
	else
	{
		if(validateOnInactive()){
			document.forms["updateEmployee"].action = "employeeMaintenance.html?method=saveUpdateEmployee";
			document.forms.updateEmployee.submit();
		}else{
			return false;
		}
	}
}

function validateMe(fld,name) { 
	var error = "";
	
	if (fld.value == null || (fld.value != null && (fld.value.length == 0 || fld.value == "Select" ))) {
		fld.style.background = 'Yellow'; 
		error = name+" field has not been filled in.\n";
	} else {
		var numaric = fld.value;
		var alphaa = numaric.charAt(0);
		var chCode = alphaa.charCodeAt(0);
		if(chCode == "32"){
			fld.style.background = 'Yellow'; 
			error = name+" should not start with space.\n";
		}
		if(name == "Password"){
			if(fld.value.length>20){
				error = name +" should not be more than 20 characters.\n";
			}else if(fld.value.length<6 && fld.value.length>0){
				error = name +" should not be less than 6 characters.\n";
			}
		}
		if(name == "Email Id"){
			var at="@";
			var dot=".";
			var lat=fld.value.indexOf(at);
			var lstr=fld.value.length;
			var ldot=fld.value.indexOf(dot);
			var invalidChar="!#$%^&*()}{][|\+=`~;:><?/,";
			var icl=invalidChar.length;
			for(var i=0;i<lstr-1;i++){
				for(var j=0;j<icl-1;j++){
					if(fld.value.substring(i,i+1) == invalidChar.substring(j,j+1)){
						fld.style.background = 'Yellow';
						error = name + " is not valid.\n";
					} 
				}
			}
			
			for(var i=0;i<lstr-2;i++){
				if(fld.value.substring(i,i+1) == dot && fld.value.substring(i+1,i+2) == dot ){
					fld.style.background = 'Yellow';
					error = name + " is not valid.\n";
				}
			}
			
			if (fld.value.indexOf(at)==-1){
				error = name + " is not valid.\n";
				
			}
			
			if (fld.value.indexOf(at)==-1 || fld.value.indexOf(at)==0 || fld.value.indexOf(at)==lstr){
				error = name + " is not valid.\n";
				
			}
			
			if (fld.value.indexOf(dot)==-1 || fld.value.indexOf(dot)==0 || fld.value.indexOf(dot)==lstr){
				error = name + " is not valid.\n";
			}
			
			if (fld.value.indexOf(at,(lat+1))!=-1){
				error = name + " is not valid.\n";
			}
			
			if (fld.value.substring(lat-1,lat)==dot || fld.value.substring(lat+1,lat+2)==dot){
				error = name + " is not valid.\n";
			}
			
			if (fld.value.indexOf(dot,(lat+2))==-1){
				error = name + " is not valid.\n";
			}
			
			if (fld.value.indexOf(" ")!=-1){
				error = name + " is not valid.\n";
			}
			
		}
	}
	return error;  
}
function cursorPosition(){
	if(document.getElementById('searchEmployeeCode')!=null){
		document.getElementById('searchEmployeeCode').focus();
	}
}

function changeShadeForEN(){
	document.getElementById('employeeName').style.background = 'White';
	document.getElementById('employeeName').focus();
}

function changeShadeForEmail(){
	document.getElementById('emailId').style.background = 'White';
	document.getElementById('emailId').focus();
}

function changeShadeForPass(){
	document.getElementById('password').style.background = 'White';
	document.getElementById('password').focus();
}

function validatePassportNo(){
	var passNo = document.getElementById('passportNo').value;
	var invalidChar=".!#$%^&*()}{][|\+=`~;:><?/, ";
	var icl=invalidChar.length;
	var error = 0;
	var lstr = passNo.length;
	for(var i=0;i<lstr;i++){
		for(var j=0;j<icl-1;j++){
			if(passNo.substring(i,i+1) == invalidChar.substring(j,j+1)){
				error = 1 ;
				
			} 
		}
	}
	if(error == 1){
		alert("Passport no. is not valid");
		document.getElementById('passportNo').focus();
	}
}


function validateEmailForUser(){
	var error = 0;
	if(document.getElementById('emailId').value != null && document.getElementById('emailId').value.length !=0 ){
		var remValue = document.getElementById('emailId').value;
		error = validateEmail(remValue);
		if( error == 1 ){
			alert("Email id is not valid.");
			document.getElementById('emailId').focus();
		}
		
	}
}

function validateReminderEmailCc(){
	if(document.getElementById('reminderEmailCc').value != null && document.getElementById('reminderEmailCc').value.length !=0 ){
		var remValue = document.getElementById('reminderEmailCc').value;
		var arr = remValue.split(",");
		var len = arr.length;
		var breakLoop = 0;
		for( var i= 0; i< arr.length ;i++){
			breakLoop = validateEmail(arr[i]);
			if( breakLoop == 1 ){
				alert("One of the reminder email ids is not valid.");
				document.getElementById('reminderEmailCc').focus();
				break;
			}
		}
	} 
}

function validateEmail(str) { 
	var error = 0;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var invalidChar="!#$%^&*()}{][|\+=`~;:><?/, ";
	var icl=invalidChar.length;
	for(var i=0;i<lstr-1;i++){
		for(var j=0;j<icl-1;j++){
			if(str.substring(i,i+1) == invalidChar.substring(j,j+1)){
				error = 1 ;
			} 
		}
	}
	
	for(var i=0;i<lstr-2;i++){
		if(str.substring(i,i+1) == dot && str.substring(i+1,i+2) == dot ){
			error = 1 ;
		}
	}
	
	if (str.indexOf(at)==-1){
		error = 1;
		
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		error = 1;
		
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		error = 1;
	}
	
	if (str.indexOf(at,(lat+1))!=-1){
		error = 1;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		error = 1;
	}
	
	if (str.indexOf(dot,(lat+2))==-1){
		error = 1;
	}
	
	if (str.indexOf(" ")!=-1){
		error = 1;
	}
	
	return error; 	 
}

function removeError(){
	
	if(document.getElementById('updateMessage') != null){
		document.getElementById('updateMessage').style.visibility='hidden';
	}
	
	if(document.getElementById('errorMessage1') != null){
		document.getElementById('errorMessage1').style.visibility='hidden';
	}
}

